const _Math = {
	//根据对角线点造出rect四点
	rect(fp,sp){
		return [fp,{
			x:fp.x,
			y:sp.y
		},sp,{
			x:sp.x,
			y:fp.y
		}]
	},
	getPolyPos(step,n){
		let fp = step[0]
		let lp = step[1]

		let dx = lp.x-fp.x
		let dy = lp.y-fp.y

		let r = Math.distance(fp,lp)
		let angle = Math.tan360(dy,dx)

		let ps = [fp,lp]
		for(let i=1;i<n;i++)
			ps.push({
				x:fp.x+r*Math.cos(angle+i*2*Math.PI/n),
				y:fp.y+r*Math.sin(angle+i*2*Math.PI/n),
			})
		return ps
	},
	rotatePos(P,O,a){
		//p目标点
		//O旋转中心
		// a角度
		let dx = P.x-O.x
		let dy = P.y-O.y

		let x = Math.cos(a)*dx - Math.sin(a)*dy+O.x
		let y = Math.sin(a)*dx + Math.cos(a)*dy+O.y
		/**
		|x'| |cos($) -sin($)| |x| |cx|	|cx|
		|  |=|				|(| |-|  |)+|  |
		|y'| |sin($)  cos($)| |y| |cy|	|cy|
		*/
		return {x,y}
	},
	centralPos(fp,sp){
		return {
			x:(fp.x+sp.x)/2,
			y:(fp.y+sp.y)/2
		}
	},
	tan360(y,x){
		let a = Math.atan2(y,x);
		return a<0?(2*Math.PI+a):a;
	},
	distance(fp,sp){
			let dx = fp.x-sp.x;
			let dy = fp.y-sp.y;
			return Math.pow((dx*dx+dy*dy),.5)
	},
	// 多散点连线 点inPath判断
	foldlineInPath(steps,pos,between){
		let c1 = false
		for(let i=0;i<steps.length-1;i++){
			let fp = steps[i]
			let sp = steps[i+1]
			let a = this.tan360(sp.y-fp.y,sp.x-fp.x)
			let c2 = true
			if(between)
				c2 = this.between(fp,sp,pos)
			let c3 = Math.point_line({x:fp.x,y:fp.y,a},pos)<fp.r
			c1 = c3&&c2
			if(c1)break;
		}
		return c1
	},
	//获取点在线段中的信息
	getLineMeta(steps,pos){
		let fp = null
		let sp = null
		let a = 0
		for(let i=0;i<steps.length-1;i++){
			fp = steps[i]
			sp = steps[i+1]
			a = Math.tan360(sp.y-fp.y,sp.x-fp.x)
			let c1 = Math.point_line({x:fp.x,y:fp.y,a},pos)<fp.r
			if(c1)break
		}
		return {
			x:fp.x,
			y:fp.y,
			a,
			sp:{
				x:sp.x,
				y:sp.y
			}
		}
	},
	//获取镜像点坐标
	mirrorPoint(pos,line){
		//line:{x,y,a}
		let {x,y} = this.calcLineCrossPos(pos,line)
		return {
			x:2*x-pos.x,
			y:2*y-pos.y
		}
	},
	dim(width,height){
		return Math.pow(width*width+height*height,.5)
	},
	//点击圆上一点，计算该点坐标
	calcCutPos1(O,P){
		let angle = Math.tan360(P.y-O.y,P.x-O.x)
		let x = O.x+O.r*Math.cos(angle)
		let y = O.y+O.r*Math.sin(angle)
		return {
			x,y,
			a:angle+Math.PI/2
		}
	},
	//圆外一点和圆的关系
	calcCutPos(O,P){
		let OP = Math.distance(O,P)
		let r = O.r
		let A = OP*OP-2*r*r
		let B = P.x*P.x + P.y*P.y
		let C = O.x*O.x + O.y*O.y
		let D = 2*(P.y-O.y)
		let E = 2*(O.x-P.x)

		let F = (A-B+C)/E
		let G = D/E
		//x = F+y*G
		//y2(G2+1)+y(2FG-2cxG-2cy)+F2+C-r2-2Fcx=0
		let a = G*G+1
		let b = 2*(F*G - G*O.x - O.y)
		let c = F*F + C - r*r - 2*F*O.x

		let iroot = b*b - 4*a*c
		let root = Math.pow(iroot,0.5)

		let y1 = (-b+root)/(2*a)
		let y2 = (-b-root)/(2*a)
		let x1 = F+G*y1
		let x2 = F+G*y2

		return [{
			x:x1,
			y:y1
		},{
			x:x2,
			y:y2
		}]

	},
	HR(a){
		let c1 = a===0
		let c2 = a===Math.PI
		let c3 = a===Math.PI*2
		return c1||c2||c3
	},
	VE(a){
		let c1 = a===Math.PI/2
		let c2 = a===Math.PI*3/2
		return c1||c2
	},
	// a[pos,pos]
	calcLineCrossPos(af,bf){
		//a b |{x,y,a}
		let x = 0
		let y = 0
		
		let aspan = Math.abs(af.a-bf.a)
		if(aspan==0||aspan==Math.PI){
			return {para:true}
		}

		let ka = Math.tan(af.a)
		let kb = Math.tan(bf.a)
		if(this.HR(af.a))
			return {
				y : af.y,
				x : (af.y-bf.y)/kb+bf.x
			}
		if(this.VE(af.a))
			return {
				x : af.x,
				y : kb*(af.x-bf.x)+bf.y
			}
		if(this.HR(bf.a))
			return {
				y : bf.y,
				x : (bf.y-af.y)/ka+af.x
			}
		if(this.VE(bf.a))
			return {
				x : bf.x,
				y : ka*(bf.x-af.x)+af.y
			}

		x = (ka*af.x-af.y-kb*bf.x+bf.y)/(ka-kb)
		y = kb*(x-bf.x)+bf.y
		return {x,y}
	},
	// 创建俩点线方程
	create2psFn(a){
		let fp = a[0]
		let sp = a[1]

		let dx = fp.x-sp.x
		let dy = fp.y-sp.y

		let k = dy/dx
		// (y-fp.y)/(x-fp.x) = k
		let expr = `return (${k})*(x-(${fp.x}))+(${fp.y})`
		return new Function("x",expr)
	},
	//三点计算圆心坐标
	calcCircleCenter(step){
		let fp = step[0]
		let sp = step[1]
		let ep = step[2]

		let efdy = fp.y-ep.y
		let efdx = fp.x-ep.x

		let esdy = sp.y-ep.y
		let esdx = sp.x-ep.x

		//法向量
		let efk = -efdx/efdy
		let esk = -esdx/esdy

		let efcx = (fp.x+ep.x)/2
		let efcy = (fp.y+ep.y)/2

		let escx = (sp.x+ep.x)/2
		let escy = (sp.y+ep.y)/2

		let x = (efk*efcx - efcy - esk*escx + escy)/(efk - esk)
		let y = efk*(x - efcx ) + efcy
		return {x,y}
	},
	//垂直平分线
	between(fp,sp,p){
		let r = fp.r
		let sx = Math.min(fp.x,sp.x)
		let mx = Math.max(fp.x,sp.x)
		let sy = Math.min(fp.y,sp.y)
		let my = Math.max(fp.y,sp.y)
		let dx = Math.abs(fp.x-sp.x)
		let dy = Math.abs(fp.y-sp.y)
		if(dx<r)return p.y>=sy&&p.y<=my
		if(dy<r)return p.x>=sx&&p.x<=mx
		return p.y>=sy&&p.y<=my&&p.x>=sx&&p.x<=mx
	},
	//点线距离
	point_line(fp,pos){
		let {x,y,a} = fp
		let dx = 1
		let dy = Math.tan(a)

		let fz = (x-pos.x)*dy-(y-pos.y)*dx
		fz = Math.abs(fz)
		let fm = Math.pow(dy*dy+dx*dx,.5)
		return fz/fm
	},
	parseFunction(expr1){
		return new Function("x","return "+expr1)
	},
	formatAngle(a){
		let p2 = Math.PI*2
		a = a>p2?a-p2:a
		a = a<0?p2-a:a
		return a
	}
}
for(let k in _Math)
	Math[k] = _Math[k]
